package com.gwamcc.adduser.controller;

import com.alibaba.fastjson.JSON;
import com.gwamcc.adduser.service.LoginService;
import com.gwamcc.adduser.vo.ResponseDataModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2020/6/9.
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/login")
public class LoginController {

    private static Logger log = LoggerFactory.getLogger(OperateController.class.getName());

    @Resource
    private LoginService loginService;

    @RequestMapping(value = "/checkUser", method = RequestMethod.GET)
    @ResponseBody
    public Object login(HttpServletRequest request) {
        String usrID = request.getParameter("userid");
        String usrPWD = request.getParameter("userpwd");

        ResponseDataModel model = new ResponseDataModel(0, "", null);
        Map<String, Object> respMap = new HashMap<String, Object>();
        respMap.put("UserID", usrID);
        if("admin".equalsIgnoreCase(usrID)) {
            String pwd = "123";
            if(usrPWD.equalsIgnoreCase(pwd)) {
                log.debug("用户认证通过！");
                Integer empid =  loginService.getEmpid(usrID);
                respMap.put("EmpID", empid);
                model.setStatus(0);
                model.setMessage("用户认证通过！");
                model.setData(respMap);
            } else {
                log.debug("密码错误，用户认证失败！");
                model.setStatus(1);
                model.setMessage("用户名或密码错误！");
                model.setData(respMap);
            }
        } else {
            log.debug("用户名错误，用户认证失败！");
            model.setStatus(1);
            model.setMessage("用户认证不通过！");
            model.setData(respMap);
        }
        return JSON.toJSON(model);
    }
}
